home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / unix / lstat.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  2KB  |  99 lines

  1.  
  2. /*
  3.  *  LSTAT call
  4.  *
  5.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  6.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  7.  *    DICE-LICENSE.TXT.
  8.  */
  9.  
  10. #include <exec/types.h>
  11. #include <dos/dos.h>
  12. #include <dos/dosextens.h>
  13. #include <clib/dos_protos.h>
  14. #include <clib/exec_protos.h>
  15. #include <sys/stat.h>
  16. #include <time.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20.  
  21. #ifndef UnixToAmigaPath
  22. #define UnixToAmigaPath(path)   path
  23. #define AmigaToUnixPath(path)   path
  24. #endif
  25.  
  26. typedef struct FileInfoBlock    FileInfoBlock;
  27. typedef struct Process        Process;
  28. typedef struct DevProc        DevProc;
  29.  
  30. extern struct DosLibrary *DOSBase;
  31.  
  32. lstat(name, st)
  33. const char *name;
  34. struct stat *st;
  35. {
  36.     char buf[256];
  37.  
  38.     if (readlink(name, buf, sizeof(buf)) >= 0) {
  39.     clrmem(st, sizeof(*st));
  40.  
  41.     st->st_mode = S_IFLNK | S_IREAD | S_IWRITE;
  42.     st->st_ctime = st->st_mtime = time(NULL);
  43.     return(0);
  44.     } else {
  45.     return(stat(UnixToAmigaPath(name), st));
  46.     }
  47. }
  48.  
  49. int
  50. readlink(path, name, max)
  51. char *path;
  52. char *name;
  53. int max;
  54. {
  55.     int r = -1;
  56.  
  57.     if (DOSBase->dl_lib.lib_Version >= 37) {
  58.     DevProc *dp = NULL;
  59.     DevProc *dp2;
  60.     short failsafe = 20;
  61.  
  62.     while (dp2 = GetDeviceProc(UnixToAmigaPath(path), dp)) {
  63.         dp = dp2;
  64.         if (ReadLink(dp->dvp_Port, dp->dvp_Lock, UnixToAmigaPath(path), name, max)) {
  65.         strcpy(name, AmigaToUnixPath(name));
  66.         r = strlen(name);
  67.         break;
  68.         }
  69.         if (--failsafe == 0)
  70.         break;
  71.         if ((dp->dvp_Flags & DVPF_ASSIGN) == 0)
  72.         break;
  73.     }
  74.     FreeDeviceProc(dp);
  75.     }
  76.     return(r);
  77. }
  78.  
  79.  
  80. #ifdef TEST
  81.  
  82. #include <fcntl.h>
  83.  
  84. main(ac, av)
  85. char *av[];
  86. {
  87.     struct stat xstat;
  88.     short i;
  89.  
  90.     for (i = 1; i < ac; ++i) {
  91.     int r = lstat(av[i], &xstat);
  92.     printf("mode r = %d fs=%d ti=%08lx mode=%08lx\n", r, xstat.st_size, xstat.st_ctime, xstat.st_mode);
  93.     }
  94.     return(0);
  95. }
  96.  
  97. #endif
  98.  
  99.